/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.stat.model.data;

import java.util.NoSuchElementException;

/**
 * A record has the request and response data.
 * @author stewari1
 */
public class Record {

    /**
     * true, if this record is to be used in model buiding. By default, this is true.
     */
    private boolean used = true;

    /** Field description */
    private final Data[] data;

    /** Field description */
    private final ModelData modelData;

    /**
     * Constructs ...
     *
     * @param data description
     * @param modelData description
     */
    public Record(Data[] data, ModelData modelData) {

        this.data      = data;
        this.modelData = modelData;
    }

    public Data[] getData() {
        return data;
    }

    /**
     * @throws java.util.NoSuchElementException if the data is not found
     * @param dataAttributeName
     * @return
     */
    public Data getData(String attributeName) {

        int dataIndex = getDataIndex(attributeName);

        if (dataIndex == -1) {

            throw new NoSuchElementException("The data with attribute name " + attributeName
                                             + "couldnot be found.");

        } else {
            return data[dataIndex];
        }
    }

    /**
     * finds the index of the corresponding data in the record using data attributes
     * @param dataAttributeName
     *
     * @param attributeName description
     * @return index of data or -1 if not found
     */
    private int getDataIndex(String attributeName) {

        DataAttribute[] dataAttributes = this.modelData.getDataAttributes();

        for (int i = 0; i < dataAttributes.length; i++) {

            if (dataAttributes[i].getName().equals(attributeName)) {
                return i;
            }
        }

        return -1;
    }

    public boolean isUsed() {
        return used;
    }

    public void setUsed(boolean used) {
        this.used = used;
    }

    /**
     *
     * @param index
     * @param val
     * @throws ArrayIndexOutOfBoundsException
     */
    public void setValue(int index, String val) {
        this.data[index].setValue(val);
    }

    /**
     * @throws java.util.NoSuchElementException if the data is not found
     * @param attributeName
     * @param val
     */
    public void setValue(String attributeName, String val) {
        getData(attributeName).setValue(val);
    }

    /**
     *
     * @return
     */
    public Record cloneDefaultInstance() {

        Data[] cloneDataValues = new Data[data.length];

        for (int i = 0; i < data.length; i++) {

            cloneDataValues[i] = data[i].cloneDefaultInstance();
        }

        return new Record(cloneDataValues, this.modelData);
    }
}
